package com.acme.bankapp.domain.accounts;

import com.acme.bankapp.exception.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {
    private double balance;

	public AbstractAccount(double amount){
		if (amount < 0) {
			throw new IllegalArgumentException("Задано отрицательное значение баланса");
		}
		this.balance = amount;
	}

    @Override
    public double getBalance() {
        return balance;
    }

    @Override
    public double maximumAmountToWithdraw() {
        return getBalance();
    }

	public void deposit(double amount) {
		balance += amount;
	}

    public void withdraw(double amount) throws NotEnoughFundsException {
        if (maximumAmountToWithdraw() >= amount) {
            balance -= amount;
        } else {
            throw new NotEnoughFundsException(maximumAmountToWithdraw());
        }
    }

    @Override
    public int compareTo(Account o) {
        return (int)(this.balance-o.getBalance());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AbstractAccount that = (AbstractAccount) o;
        return that.balance ==  balance;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(balance);
        return (int) (temp ^ (temp >>> 32));
    }

    @Override
    public String toString() {
        return "Balance: " + getBalance() + "; Overdraft: " + (maximumAmountToWithdraw() - getBalance());
    }
}
